package org.cc.media.player;

import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.SourceDataLine;



import org.apache.commons.io.IOUtils;

public class CCStreamPlayer implements Runnable {
	
	private static final Integer BUFFER_SIZE = 32768;
	
	public CCStreamPlayer(AudioInputStream stream, SourceDataLine line) {
		super();
		this.stream = stream;
		this.line = line;
	}
	
	public CCStreamPlayer() {
		super();
	}
	
	private AudioInputStream stream;
	private SourceDataLine line;
	private Boolean play = true;
	private Integer bytesRead = -1;
	private byte[] bytesBuffer = new byte[BUFFER_SIZE];
	
	@Override
	public void run() {
		try {
			while ((bytesRead = stream.read(bytesBuffer)) > -1) {
				if (play) {
					line.write(bytesBuffer, 0, bytesRead);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(stream);
		}
	}
	
	public Boolean isPlaying() {
		return play;
	}
	
	public Boolean isPaused() {
		return play;
	}
	
	public void play() {
		play = true;
	}
	
	public void pause() {
		play = false;
	
	}
	
	public AudioInputStream getStream() {
		return stream;
	}
	
	public void setStream(AudioInputStream stream) {
		this.stream = stream;
	}
	
	public SourceDataLine getLine() {
		return line;
	}
	
	public void setLine(SourceDataLine line) {
		this.line = line;
	}
	
	
}
